<?php

namespace app\common\model;

use think\Exception;
use think\facade\Log;

class Base extends TimeModel
{
    protected $deleteTime = "delete_time";

    /**
     * @param int $id
     * @param string $field
     * @param array|string $with
     * @return array
     * @throws Exception
     */
    public function getBaseById(int $id = 0, string $field = '*', $with = null): array
    {
        try {
            $result = $this->field($field)
                ->where('id', $id)
                ->with($with)
                ->find();
        } catch (\Exception $e) {
            Log::error($e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            throw new Exception("内部发生错误");
        }
        if ($result) {
            return $result->toArray();
        } else {
            return [];
        }
    }

    /**
     * @param array $where
     * @param string $field
     * @return array
     * @throws Exception
     */
    public function getBaseByWhereOne(array $where = [], string $field = '*')
    {
        try {
            $result = $this->field($field)
                ->where($where)
                ->find();
        } catch (\Exception $e) {
            Log::error($e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            throw new Exception("内部发生错误");
        }
        if ($result) {
            return $result->toArray();
        } else {
            return [];
        }
    }

    /**
     * 根据条件查询指定数据
     * @param array $where
     * @param string $field
     * @param string $order
     * @return array
     * @throws Exception
     */
    public function getBaseByWhere(array $where = [], string $field = '*', string $order = 'id desc'): array
    {
        try {
            $result = $this->field($field)
                ->where($where)
                ->order($order)
                ->select();
        } catch (\Exception $e) {
            Log::error($e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            throw new Exception("内部发生错误");
        }
        if ($result) {
            return $result->toArray();
        } else {
            return [];
        }
    }

    /**
     * 根据条件查询指定数据
     * @param array $where
     * @param string $field
     * @return array
     * @throws Exception
     */
    public function getBaseByWhereCount(array $where = [], string $field = '*')
    {
        try {
            $result = $this->field($field)
                ->where($where)
                ->count();
        } catch (\Exception $e) {
            Log::error($e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            throw new Exception("内部发生错误");
        }

        return $result;

    }

    /**
     * @param array $where
     * @param string $field
     * @param string $orderBy
     * @return array
     * @throws Exception
     */
    public function getBaseByList(array $where = [], string $field = '*', string $orderBy = "id desc")
    {
        try {
            $result = $this->field($field)
                ->where($where)
                ->order($orderBy)
                ->select();
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        if ($result) {
            return $result->toArray();
        } else {
            return [];
        }
    }

    /**
     * 分页方法
     * @param array $where
     * @param int $page
     * @param int $limit
     * @param string $field
     * @param string $orderBy
     * @param array|string $with
     * @return array
     * @throws Exception
     */
    public function getBaseByListPage(array $where = [], int $page = 1, int $limit = 10, string $field = '*', string $orderBy = 'id desc',$with = null): array
    {
        try {
            $result = $this->field($field)
                ->where($where)
                ->order($orderBy)
                ->with($with)
                ->page($page, $limit)
                ->select();
        } catch (\Exception $e) {
            Log::error($e->getMessage()." ".$e->getLine()." ".$e->getFile());
            throw new Exception("查询失败");
        }
        if ($result) {
            return $result->toArray();
        } else {
            return [];
        }
    }

    /**
     * @param array $where
     * @param string $field
     * @return Base|array|mixed|\think\Model|null
     * @throws Exception
     */
    public function getBaseByListFind(array $where = [], string $field = '*')
    {
        try {
            $result = $this->field($field)
                ->where($where)
                ->find();
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        return $result;
    }

    /**
     * 获取商品规格值
     * @param $value
     * @return array|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function getGetSpecsValueAttr($value)
    {
        if (empty($value)) {
            return '';
        }
        $ids = explode("-", $value);
        $res = (new MallSpecsValue())->where('id', 'in', $ids)
            ->select()->toArray();
        return array_column($res, 'title');
    }


}